package com.app.newiptv.adaper;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.app.newiptv.R;
import com.app.newiptv.base.MyApplication;
import com.app.newiptv.presenter.SitcomOrderPresenter;

/**
 * Created by Shinelon on 2017/9/15.
 */

public class SitcomAdapter extends RecyclerView.Adapter<SitcomAdapter.MainRecycleViewHolder> {
    private SitcomOrderPresenter presenter;
    private int count;

    public SitcomAdapter(SitcomOrderPresenter presenter, int count) {
        this.presenter = presenter;
        this.count = count;
    }

    @Override
    public MainRecycleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_sitcom_view, parent, false);
        MainRecycleViewHolder mainRecycleViewHolder = new MainRecycleViewHolder(view);
        return mainRecycleViewHolder;
    }

    @Override
    public void onBindViewHolder(MainRecycleViewHolder holder, int position) {
        presenter.onBindSitcomChanelView(holder, position);
        holder.setPosition(position);
    }

    @Override
    public int getItemCount() {
        return count;
    }

    public class MainRecycleViewHolder extends RecyclerView.ViewHolder implements SitcomOrderPresenter.MainViewSitcomHolder {

        private TextView mBtn_xj;
        private int position;
        private String[] sitcomArr;
        private String index;

        public MainRecycleViewHolder(View itemView) {
            super(itemView);
            mBtn_xj = (TextView) itemView.findViewById(R.id.btn_xj);
            mBtn_xj.setTypeface(MyApplication.face);
            itemView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    if (b) {
                        mBtn_xj.setBackgroundResource(R.mipmap.se_item_bg_select);
                    } else {
                        boolean isExis = false;
                        if (sitcomArr != null && sitcomArr.length > 0) {
                            for (int i = 0; i < sitcomArr.length; i++) {
                                if (index.equals(sitcomArr[i])) {
                                    isExis = true;
                                }
                            }
                        }
                        if (isExis) {
                            mBtn_xj.setBackgroundResource(R.mipmap.se_item_bg_focused);
                        } else {
                            mBtn_xj.setBackgroundResource(R.mipmap.se_item_bg_normal);
                        }
                    }
                }
            });
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    presenter.toPlay(position);
                }
            });
        }

        @Override
        public void setNumber(String index, String[] sitcomArr) {
            if (index != null) {
                this.sitcomArr = sitcomArr;
                this.index = index;
                boolean isExis = false;
                if (sitcomArr != null && sitcomArr.length > 0) {
                    for (int i = 0; i < sitcomArr.length; i++) {
                        if (index.equals(sitcomArr[i])) {
                            isExis = true;
                        }
                    }
                }
                if (isExis) {
                    mBtn_xj.setBackgroundResource(R.mipmap.se_item_bg_focused);
                } else {
                    mBtn_xj.setBackgroundResource(R.mipmap.se_item_bg_normal);
                }
                int textLength = index.length();
                if (textLength < 3) {
                    mBtn_xj.setTextSize(16);
                } else if (textLength >= 3 && textLength <= 8) {
                    mBtn_xj.setTextSize(14);
                } else {
                    mBtn_xj.setTextSize(12);
                }
                mBtn_xj.setText(index);
            }
        }

        public void setPosition(int position) {
            this.position = position;
        }
    }
}
